

/******************************************************************************
* Copyright (C) 2021, BBI Technology Co.,Ltd All rights reserved.
*
* Author: Embedded software group
*
* file： api_valvemotor.c 
*
*
*Note：此文件位于对象层
 ******************************************************************************/
 
#include "api_valve.h"

/**
 *****************************************************************************
 ** \brief 阀门初始化
 **
 ** \param 
 ** 
 ** \retval 无                                     
 *****************************************************************************/
void API_Valve_Init(void)
{
	BSP_MOTOR_GPIO_init();
}


/**
 *****************************************************************************
 ** \brief API_Valve_Close
 **
 **
 ** \param void
 ** 
 ** \retval false:未执行或执行失败  
 **         true : 执行成功
 *****************************************************************************/
bool API_Valve_Close(void)
{
	bool ret = false;
	int delay1ms_cnt = 0;
	vTaskDelay(10); 
	if(V_Closed == API_Valve_StateRead())
	{
		return ret;
	}	
	MotorMoveReverse;	
	while(V_Closed != API_Valve_StateRead())
	{
		vTaskDelay(1);
		delay1ms_cnt++;			
		if(delay1ms_cnt > 10000)
		{
			ret = false;
			break;
		}
		ret = true;
	};	
	MotorStop;
	return ret;
}

/**
 *****************************************************************************
 ** \brief API_Valve_Open
 **
 **
 ** \param void
 ** 
 ** \retval false:未执行或执行失败  
 **         true : 执行成功
 *****************************************************************************/
bool API_Valve_Open(void)
{
	bool ret = false;
	int delay1ms_cnt = 0;	
	vTaskDelay(10); 
	if(V_Opened == API_Valve_StateRead())
	{
		return ret;
	}		
	MotorMoveForward;		
	while(V_Opened != API_Valve_StateRead())	
	{
		vTaskDelay(1);
		delay1ms_cnt++;	
		if(delay1ms_cnt > 10000)  
		{
			ret = false;
			break;
		}
		ret = true;
	};	
	MotorStop;
	return ret;
}
/**
 ***********************************************************************
 ** \brief  读取阀门到位开关的状态
 **
 **
 ** \param 1 :void
 ** 
 ** \retval   状态码                                 
 ***********************************************************************/
uint8_t API_Valve_StateRead(void)
{	
    uint8_t Status1;
    uint8_t Status2;
    BSP_VALVE_GPIO_Read(&Status1,&Status2);	
	if((Status1 == TRUE) && (Status2 == FALSE))
	{
		return V_Opened;	
	}
	else if((Status1 == FALSE) && (Status2 == TRUE))
	{
		return V_Closed;	
	}
	else if((Status1 == TRUE) && (Status2 == TRUE))
	{
		return V_BothHigh;	
	}
	else 
	{
	    return V_BothLow;	
	}
}



